#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

class Person{
public:
    Person(const char * name, int age)
    :_name(new char[strlen(name)+1]())
    ,_age(age){
        strcpy(_name,name);
        //cout << "Person(const char* name,int age)" << endl;
    }
    Person(const Person & rhs)
    :_name(new char[strlen(rhs._name)+1]())
    ,_age(rhs._age){
        strcpy(_name,rhs._name);
    }
    Person & operator=(const Person & rhs){
        if(this != &rhs){
            delete [] _name;
            _name = new char[strlen(rhs._name)+1]();
            strcpy(_name,rhs._name);
            _age = rhs._age;
        }
        return *this;
    }
    ~Person(){
        if(_name){
            delete [] _name;
            _name = nullptr;
        }
    }
    void display(){
        cout << "name:" << _name << endl;
        cout << "age:" << _age<<endl;
    }
protected:
    char* _name;
    int _age;
};

class Employee:public Person{
public:
    Employee(const char* name,int age,const char* department,double salay)
    :Person(name,age)
    ,_department(new char[strlen(department)+1]())
    ,_salay(salay)
    {
        strcpy(_department,department);
        _salay_sum += _salay;
        _count += 1;
    }

    Employee(const Employee & rhs)
    :Person(rhs)
    ,_department(new char[strlen(rhs._department)+1]())
    ,_salay(rhs._salay)
    {
        strcpy(_department,rhs._department);
        _salay_sum += _salay;
        _count += 1;
    }

    Employee & operator=(const Employee & rhs){
        if(this != &rhs){
            Person::operator=(rhs);
            _salay_sum -= _salay;
            delete [] _department;
            _department = new char[strlen(rhs._department)+1]();
            strcpy(_department,rhs._department);
            _salay = rhs._salay;
            _salay_sum += _salay;
        }
        return *this;
    }
    void display(){
        cout << "member_count:" << _count << endl;
        cout<< _name <<" "<< _age <<" "<< _department<<" "<< _salay <<endl;
        cout << "sum salay:" << _salay_sum << endl;
        cout << "average salay:" << _salay_sum/_count << endl;
        cout << "-----------------------------------"<< endl;
    }
    
    ~Employee(){
        if(_department){
            delete [] _department;
            _department = nullptr;
            _count -= 1;
            _salay_sum -= _salay;
        }
    }
private:
    char* _department;
    double _salay;
    static int _count; 
    static double _salay_sum;
};
int Employee::_count = 0;
double Employee::_salay_sum = 0;

void test(){
    Person p1("zhangsan",18);
    p1.display();
    Person p2(p1);
    p2.display();
    Person p3("lisi",19);
    p3.display();
    p2 = p3;
    p2.display();
}
void test2(){
    Employee e1("zhangsan",18,"IT",8000);
    e1.display();
    Employee e2(e1);
    e2.display();
    Employee e3("lisi",20,"HR",9000);
    e1 = e3;
    e1.display();
}

int main()
{
    test2();
    return 0;
}

